﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace WpfAdvControls.StatusPanel
{
    /// <summary>
    ///     Interaction logic for StatusBox.xaml
    /// </summary>
    public partial class StatusBox : UserControl
    {
        // Using a DependencyProperty as the backing store for Status.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof (bool), typeof (StatusBox),
                new FrameworkPropertyMetadata(false, OnChangeStatus));

        // Using a DependencyProperty as the backing store for StatusText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StatusTextProperty =
            DependencyProperty.Register("StatusText", typeof (string), typeof (StatusBox),
                new FrameworkPropertyMetadata(string.Empty, OnChangeStatus));

        private readonly BoolBrushConverter converter;

        public StatusBox()
        {
            InitializeComponent();
            var binding = new Binding();
            converter = (BoolBrushConverter) Resources["BoolBrushConverter1"];
            UpdateView();
        }

        public bool Status
        {
            get { return (bool) GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }

        public string StatusText
        {
            get { return (string) GetValue(StatusTextProperty); }
            set { SetValue(StatusTextProperty, value); }
        }

        public static void OnChangeStatus(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((StatusBox) d).UpdateView();
        }

        private void UpdateView()
        {
            statusRect.ToolTip = StatusText;
            statusRect.Fill = (Brush) (converter.Convert(Status, typeof (Brush), null, null));
        }
    }
}